package com.mzy.linear_struct.代码随想录.回溯;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * @Author: mizy3
 * @Date: 2024/08/15/下午11:33
 * @Description:
 */
public class Pro216组合总和III {
    LinkedList<Integer> path = new LinkedList<>();
    List<List<Integer>> ans = new ArrayList<>();

    public List<List<Integer>> combinationSum3(int k, int n) {

        backtracking(k, n, 0, 1);

        return ans;
    }

    private void backtracking(int k, int target, int sum, int startIndex) {

        if (target == sum && path.size() == k) {
            ans.add(new ArrayList<>(path));
            return;
        }

        for (int i = startIndex; i <= 9 - (k - path.size()) + 1; i++) {
            path.add(i);
            sum += i;
            backtracking(k, target, sum, i + 1);
            sum -= i;
            path.removeLast();
        }
    }

}
